/**
 * Created by zwq on 2017/7/11 0011.
 */

$(function(){
   new uploadImgFun();
});
function uploadImgFun(){
    this.init();
}
uploadImgFun.prototype={
    init:function(){
        this.uploadImgClick();
    },
    uploadImgClick:function(){
        var slf=this;

        $("#uploadImg").bind("click",function(){

            $("#uploadInput").click();
            slf.uploadInputClick();
        })
    },
    uploadInputClick:function(){
        var self=this;

        $("#uploadInput").bind("change",function(){
            self.uploadInputBind();
        })
    },
    uploadInputBind:function(){
        var self=this;
        var pic = document.getElementById("uploadImg"),
            file = document.getElementById("uploadInput");

        var ext=file.value.substring(file.value.lastIndexOf(".")+1).toLowerCase();

        if(ext!='png' && ext!='jpg' && ext!='jpeg' && ext!='png' && ext!='gif'){
            alert("图片格式不正确");
            return;
        }
        var isIE = navigator.userAgent.match(/MSIE/)!= null,
            isIE6 = navigator.userAgent.match(/MSIE 6.0/)!= null;

        if(isIE) {
            file.select();
            var reallocalpath = document.selection.createRange().text;

            if (isIE6) {
                pic.src = reallocalpath;
                pic.style.height="100px";
            }else {
                pic.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='image',src=\"" + reallocalpath + "\")";
                pic.src = '';
            }
        }else {
            self.uploadImg(file);
        }
    },
    uploadImg:function(file){
        var file = file.files[0];
        var reader = new FileReader();
        var size=Math.round((file.size)/1024);//图片大小默认是以字节为单位，转为KB后取整
        reader.readAsDataURL(file);

        if(size>500){

            alert("图片大小不能超过500KB");
            return;
        }
        reader.onload = function(e){

            var pic = document.getElementById("uploadImg");
            pic.src=this.result;
            console.log(size);//图片大小
        }
    }

};

